'''
Created on 2011-10-27

@author: tony.li.fly@gmail.com
'''
from xyh.oa.models import ContainerType
from django.http import HttpResponse
from django.utils import simplejson as json

def listall(request):
    result = []
    for c in ContainerType.objects.order_by('size').all():
        result.append({'id':c.id,'size':c.size,'max_weigth':c.max_weigth,'max_volume':c.max_volume})
    return HttpResponse(json.dumps(result))


def delContainer(request,container_id):
    container = ContainerType.objects.get(pk=container_id)
    container.delete()
    return HttpResponse()
    
def addContainer(request):
    container = ContainerType(size=request.POST['size'],max_weigth=request.POST['max_weigth'],max_volume=request.POST['max_volume'])
    container.save()
    return HttpResponse()

def updateContainer(request):
    container = ContainerType.objects.get(pk = request.POST['id'])
    container.size = request.POST['size']
    if request.POST.get('max_weigth')=='':
        container.max_weigth = None
    else :
        container.max_weigth = request.POST.get('max_weigth')
    if request.POST.get('max_volume')=='':
        container.max_volume = None
    else :
        container.max_volume = request.POST['max_volume']
    container.save()
    return HttpResponse()